﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marin.Entities;
using Marin.Service;

namespace Marin.UI
{
    public partial class frmCategorias : Form
    {
        private ICategoriaService catService;
        private bool IsNew = true;
        public frmCategorias()
        {
            InitializeComponent();
            catService= new CategoriaService();
        }

        public void AddCategoria()
        {
            categoriaBindingSource.AddNew();
        }

        public Categoria categoria { get; set; }
        public void UpdateCategoria()
        {
            IsNew = false;
            categoriaBindingSource.DataSource = categoria;
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var categoria = categoriaBindingSource.Current as Categoria;
            var msg = "ERROR";

            if (categoria!=null)
            {
                categoria.Descripcion = descripcionTextBox.Text.ToUpper();

                if (IsNew)
                {
                    try
                    {
                        catService.AddCategoria(categoria);
                        MessageBox.Show("Registro Correcto", 
                            "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    try
                    {
                        catService.UpdateCategoria(categoria);
                        UpdateCategoria();
                        MessageBox.Show("Modificación Correcta",
                            "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, 
                            "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
